/**
 * Copyright (c) 2010 CWI Amsterdam and others. All rights reserved.
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     CWI Amsterdam - initial API and implementation
 *
 * $Id$
 */
package nl.cwi.arne.impl;

import java.util.Collection;

import nl.cwi.arne.Activation;
import nl.cwi.arne.Network;
import nl.cwi.arne.NetworkActivation;
import nl.cwi.arne.Node;
import nl.cwi.arne.NodeActivation;
import nl.cwi.arne.factory.ArnePackage;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.MinimalEObjectImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

/**
 * @generated
 */
public class NetworkActivationImpl extends MinimalEObjectImpl implements NetworkActivation {
	
	/**
	 * Convenience constructor.
	 * @param network Network to be used.
	 */
	public NetworkActivationImpl(Network network) {
		setNetwork(network);
	}
	
	/**
	 * @generated NOT
	 */
	public NodeActivation getNodeActivation(Node node) {
		for (NodeActivation activation : getNodeActivations()) {
			if (node==activation.getNode()) return activation;
		}
		return null;
	}

	/**
	 * @generated NOT
	 */
	public void putNodeActivation(NodeActivation activation) {
		Node node = activation.getNode();
		for (int i=0; i<getNodeActivations().size(); i++) {
			if (getNodeActivations().get(i).getNode()==node) {
				getNodeActivations().set(i, activation);
				return;
			}
		}
		getNodeActivations().add(activation);
	}
	
	/**
	 * @generated NOT
	 */
	public boolean isMonotonic() {
		for (NodeActivation activation : getNodeActivations()) {
			if (!activation.isMonotonic()) return false;
		}
		return true;
	}

	/**
	 * @generated NOT
	 */
	public Activation getCopy() {
		NetworkActivation copy = new NetworkActivationImpl(network);
		for (NodeActivation activation : getNodeActivations()) {
			copy.getNodeActivations().add((NodeActivation) activation.getCopy());
		}
		return copy;
	}

	/**
	 * @generated NOT
	 */
	@Override
	public String toString() {
		String result = new String();
		for (int i=0; i<getNodeActivations().size(); i++) {
			NodeActivation activation = getNodeActivations().get(i);
			result = result + activation.getNode().getName() + ":[" + activation + "]";
			if (i<getNodeActivations().size()-1) result = result + ", ";
		}
		return result;
	}
	
	/* ---------------------------------------------------------------- *
	 * GENERATED CODE.                                                  *
	 * Do not edit below this line. If you need to edit, move it above  *
	 * this line and change the '@generated'-tag to '@generated NOT'.   *
	 * ---------------------------------------------------------------- */
	
	/**
	 * The cached value of the '{@link #getNodeActivations() <em>Node Activations</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getNodeActivations()
	 * @generated
	 * @ordered
	 */
	protected EList<NodeActivation> nodeActivations;

	/**
	 * The cached value of the '{@link #getNetwork() <em>Network</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getNetwork()
	 * @generated
	 * @ordered
	 */
	protected Network network;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected NetworkActivationImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return ArnePackage.Literals.NETWORK_ACTIVATION;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Network getNetwork() {
		if (network != null && network.eIsProxy()) {
			InternalEObject oldNetwork = (InternalEObject)network;
			network = (Network)eResolveProxy(oldNetwork);
			if (network != oldNetwork) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, ArnePackage.NETWORK_ACTIVATION__NETWORK, oldNetwork, network));
			}
		}
		return network;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Network basicGetNetwork() {
		return network;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setNetwork(Network newNetwork) {
		Network oldNetwork = network;
		network = newNetwork;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ArnePackage.NETWORK_ACTIVATION__NETWORK, oldNetwork, network));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<NodeActivation> getNodeActivations() {
		if (nodeActivations == null) {
			nodeActivations = new EObjectContainmentEList<NodeActivation>(NodeActivation.class, this, ArnePackage.NETWORK_ACTIVATION__NODE_ACTIVATIONS);
		}
		return nodeActivations;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ArnePackage.NETWORK_ACTIVATION__NODE_ACTIVATIONS:
				return ((InternalEList<?>)getNodeActivations()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case ArnePackage.NETWORK_ACTIVATION__NODE_ACTIVATIONS:
				return getNodeActivations();
			case ArnePackage.NETWORK_ACTIVATION__NETWORK:
				if (resolve) return getNetwork();
				return basicGetNetwork();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case ArnePackage.NETWORK_ACTIVATION__NODE_ACTIVATIONS:
				getNodeActivations().clear();
				getNodeActivations().addAll((Collection<? extends NodeActivation>)newValue);
				return;
			case ArnePackage.NETWORK_ACTIVATION__NETWORK:
				setNetwork((Network)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case ArnePackage.NETWORK_ACTIVATION__NODE_ACTIVATIONS:
				getNodeActivations().clear();
				return;
			case ArnePackage.NETWORK_ACTIVATION__NETWORK:
				setNetwork((Network)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case ArnePackage.NETWORK_ACTIVATION__NODE_ACTIVATIONS:
				return nodeActivations != null && !nodeActivations.isEmpty();
			case ArnePackage.NETWORK_ACTIVATION__NETWORK:
				return network != null;
		}
		return super.eIsSet(featureID);
	}

} //NetworkActivationImpl
